package icu.wdtt.Utils;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.temp.SaTempUtil;
import icu.wdtt.model.FileBase64;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Properties;

@Component
@ConfigurationProperties("email")
public class EmailSendUtils {
    // 邮箱服务器
    private  String host;
    //邮箱端口
    private  Integer port;
    // 这个是你的邮箱用户名
    private  String username;
    // 你的邮箱授权码 不是邮箱密码
    private  String password;
    //邮件来自来自那里
    private  String mail_from;
    //邮件主题
    private String mail_subject;
    //发件人名称
    private  String personalName = "";

    private  String mail_body = "您的验证码是:";
    public EmailSendUtils() {
    }

    /**
     * 此段代码用来发送电子邮件
     * @param mail_to 收件人地址

     * @return
     */
    public FileBase64 send(String mail_to) {
        try {
            Properties props = new Properties();
            // 进行邮件服务器用户认证
            Authenticator auth = new Email_Autherticator();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", port);
            Session session = Session.getInstance(props, auth);
            // 设置session,和邮件服务器进行通讯。
            MimeMessage message = new MimeMessage(session);
            message.setSubject(mail_subject); // 设置邮件主题
            //获取邮箱验证码
            String emailCode = getCode();
            message.setText(mail_body +emailCode+"打死也不要告诉别人!~ 有效期为30分钟! \n --哈哈网盘"); // 设置邮件正文

            message.setSentDate(new Date()); // 设置邮件发送日期
            Address address = new InternetAddress(mail_from, personalName);
            message.setFrom(address); // 设置邮件发送者的邮箱地址
            Address toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址
            message.addRecipient(Message.RecipientType.TO, toAddress);
            Transport.send(message); // 发送邮件
            //设置邮箱验证码的token
            String emailId = SaTempUtil.createToken(emailCode, 30 * 60);
            //返回信息
            return new FileBase64("邮箱验证码",null,"0","txt",
                    "emailId",emailId
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用来进行服务器对用户的认证
     */
    public  class Email_Autherticator extends Authenticator {
        public Email_Autherticator() {
            super();
        }

        public Email_Autherticator(String user, String pwd) {
            super();
            username = user;
            password = pwd;
        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }

    /**
     * 生成验证码
     * @return
     */
    public  String getCode(){
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            code.append((int)(Math.random()*10));
        }
        return code.toString();
    }


    /**
     * 验证验证码
     * @param code
     * @param emailId
     * @return
     */
    public  Boolean isCode(String code, String emailId){
        //根据token获取验证码
        String emailCode =  SaTempUtil.parseToken(emailId,String.class);
        if(emailCode == null){
            return false;
        }
        if(emailCode.equalsIgnoreCase(code)){
            return true;
        }
        return false;
    }

    /**
     * 移除验证码
     * @param emailId
     */
    public void delCode(@NotNull String emailId){
        //根据token销毁验证码
        SaTempDelUtils.delToken(emailId);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMail_from() {
        return mail_from;
    }

    public void setMail_from(String mail_from) {
        this.mail_from = mail_from;
    }

    public String getMail_subject() {
        return mail_subject;
    }

    public void setMail_subject(String mail_subject) {
        this.mail_subject = mail_subject;
    }

    public String getPersonalName() {
        return personalName;
    }

    public void setPersonalName(String personalName) {
        this.personalName = personalName;
    }

    public String getMail_body() {
        return mail_body;
    }

    public void setMail_body(String mail_body) {
        this.mail_body = mail_body;
    }
}